package com.leyou.item.service.iml;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

     @Autowired
     private CategoryBrandService categoryBrandService;
    @Override
    public PageDTO<BrandDTO> pageQuery(Integer page, Integer rows,String key) {
        //创建分页条件对象  mp插件
        Page queryPage = new Page(page, rows);
        //构造条件
        //不为空
        boolean notEmpty = StringUtils.isNoneBlank(key);
        //传入querypagef->反填充query (判断条件notEmpty是否符合 符合则执行查询条件)
        //select * from where key = key limit 0,5
        page(queryPage, new QueryWrapper<Brand>()
                .like(notEmpty,"name",key)
                .or()
                .eq(notEmpty,"letter",key)
        );
        //获取总数,页数,数据转化返回
        return new PageDTO<BrandDTO>(queryPage.getTotal(), queryPage.getPages(), BrandDTO.convertEntityList(queryPage.getRecords()));
    }

    @Override
    public void addBrand(BrandDTO brandDTO) {
        //将DTO转化成entity
        Brand brand = brandDTO.toEntity(Brand.class);
        //插入数据
        this.save(brand);
        //向中间表插入数据(传来的数据有Category的id(面板为选择项))
        //创建一个空对象为批量插入做准备
        List<Long> categoryIds = brandDTO.getCategoryIds();
        //循环列表放到新的集合
        List<CategoryBrand> list=new ArrayList<>();
        for (Long categoryId : categoryIds) {
            //of为Category构造器名字
            list.add(CategoryBrand.of(categoryId,brand.getId()));
        }
        this.categoryBrandService.saveBatch(list);
    }

    @Override
    public void updateBrand(BrandDTO brandDTO) {
        //将DTO转化成entity
        Brand brand = brandDTO.toEntity(Brand.class);
        //更新
        this.updateById(brand);
        //更新中间表
        //先根据brandID删除中间表的连接表
        categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",brand.getId()));
        //再根据beand的id在中间表添加
        //向中间表插入数据(传来的数据有Category的id(面板为选着项(数据回显)))
        List<Long> categoryIds = brandDTO.getCategoryIds();
        //循环列表放到新的集合
        //创建一个空对象为批量插入做准备
        List<CategoryBrand> list=new ArrayList<>();
        for (Long categoryId : categoryIds) {
            //of为Category构造器名字
            list.add(CategoryBrand.of(categoryId,brand.getId()));
        }
        this.categoryBrandService.saveBatch(list);

    }

    @Override
    public List<BrandDTO> queryBrandByIds(List<Long> ids) {

        return BrandDTO.convertEntityList(listByIds(ids));
    }

    @Override
    public BrandDTO queryBrandById(Long id) {
        return new BrandDTO(this.getById(id));
    }

    @Override
    public void deleteById(Long bid) {
        this.removeById(bid);
        //delete from tb_category_brand where brand_id = bid
        this.categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",bid));
    }

}